
package com.lty.spider.util;

/**
 * create by  lty
 * on  2019-06-20  10:50
 */
import java.sql.*;       //导入java.sql包
public class DbUtil {
      private Connection con;      //声明Connection对象
        public Connection getConnection(){    //建立返回值为Connection的方法
            try {                             //加载数据库驱动
                Class.forName("org.sqlite.JDBC");
                System.out.println("数据库驱动加载成功");
            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
            try {                 //通过访问数据库的URL获取数据库连接对象
                con=DriverManager.getConnection("jdbc:sqlite:movie.db");
                System.out.println("数据库连接成功");
            }catch(SQLException e) {
                e.printStackTrace();
            }
            return con;           //按方法要求放回一个Connection对象
        }


    public static void main(String[] args) throws SQLException {
        DbUtil dbUtil = new DbUtil();
        Connection connection = dbUtil.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("create table if not exists movie(id integer PRIMARY KEY autoincrement,title varchar(100) ,content varchar(100),link varchar(100))");
        System.out.println(resultSet);

        connection.close();
    }

}
